package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ghost.GhostPatrons;
import com.yanqu.road.entity.activity.ghost.GhostUserData;
import com.yanqu.road.entity.activity.ghost.config.GhostBossConfig;
import com.yanqu.road.entity.activity.ghost.config.GhostBoxConfig;
import com.yanqu.road.entity.activity.ghost.config.GhostConfig;
import com.yanqu.road.entity.activity.ghost.config.GhostGuessConfig;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;

import java.util.List;

public class GhostPb {

    public static GhostActivityProto.SyncGameConfigRespMsg.Builder parseSyncGameConfigRespMsg(GhostConfig config) {
        GhostActivityProto.SyncGameConfigRespMsg.Builder builder = GhostActivityProto.SyncGameConfigRespMsg.newBuilder();
        builder.setMaxEnergy(config.getMaxEnergy());
        builder.setEnergyRecoveryTime(config.getEnergyRecoveryTime());
        builder.setBuyEnergyBaseIngots(config.getBuyEnergyBaseIngots());
        builder.setBuyEnergyIncrIngots(config.getBuyEnergyIncrIngots());
        builder.setBuyEnergyGetCount(config.getBuyEnergyGetCount());
        builder.setOnceEnergyBuyTimes(config.getOnceEnergyBuyTimes());

        builder.setMaxAliveNewsCount(config.getMaxAliveNewsCount());
        builder.setMaxDeadNewsCount(config.getMaxDeadNewsCount());

        for (GhostBossConfig bossConfig : config.getBossConfigList()) {
            builder.addBossConfigList(parseGhostBossConfigMsg(bossConfig, null, null));
        }
        builder.setFreeAttackTimes(config.getFreeAttackTimes());
        builder.addAllAttackCostItemCountList(config.getAttackCostItemCountList());
        builder.setAttackCostItemId(config.getAttackCostItemId());

        builder.setBoxExpireTime(config.getBoxExpireTime());

        builder.setGuessCost(config.getGuessCost());

        builder.setAttackBuffId(config.getAttackBuffId());

        builder.setStartOperationTime(config.getStartOperationTime());
        builder.setEndOperationTime(config.getEndOperationTime());

        builder.setTokenId(config.getTokenId());
        builder.setScoreId(config.getScoreId());

        for (GhostBoxConfig boxConfig : config.getBoxConfigList()) {
            builder.addBoxConfigList(parseGhostBoxConfigMsg(boxConfig));
        }

        builder.setGuessNpcId(config.getGuessNpcId());

        for (GhostGuessConfig guessConfig : config.getGuessConfigList()) {
            builder.addGuessConfigList(parseGhostGuessRewardConfigMsg(guessConfig));
        }

        builder.setAttackBuffSwitch(config.getAttackBuffSwitch());

        builder.setBigBossStageCount(config.getBigBossStageCount());
        builder.addAllBigBossStageRewardList(config.getBigBossStageRewardList());

        builder.addAllRefreshBossTimes(config.getRefreshTimeList());

        builder.setAutoBattleLimit(config.getAutoBattleLimit());
        return builder;
    }

    public static GhostActivityProto.GhostGuessRewardConfigMsg.Builder parseGhostGuessRewardConfigMsg(GhostGuessConfig config) {
        GhostActivityProto.GhostGuessRewardConfigMsg.Builder builder = GhostActivityProto.GhostGuessRewardConfigMsg.newBuilder();
        builder.setRealReward(config.getRealReward());
        builder.setFakeReward(config.getFakeReward());
        return builder;
    }

    public static GhostActivityProto.GhostBoxConfigMsg.Builder parseGhostBoxConfigMsg(GhostBoxConfig config) {
        GhostActivityProto.GhostBoxConfigMsg.Builder builder = GhostActivityProto.GhostBoxConfigMsg.newBuilder();
        builder.setBoxItemId(config.getBoxItemId());
        builder.setCost(config.getOpenCost());
        return builder;
    }

    /**
     * 生成boss配置
     *
     * @param typeList 没有就传null
     * @param hpList   没有就传null
     */
    public static GhostActivityProto.GhostBossConfigMsg.Builder parseGhostBossConfigMsg(GhostBossConfig bossConfig, List<Integer> typeList, List<Long> hpList) {
        GhostActivityProto.GhostBossConfigMsg.Builder builder = GhostActivityProto.GhostBossConfigMsg.newBuilder();
        builder.setBossType(bossConfig.getBossType());
        builder.setKillReward(bossConfig.getRewards());
        builder.setNpcId(bossConfig.getNpcId());
        builder.addAllRandomRewardList(bossConfig.getRandomRewardList());
        builder.setIsBigBoss(bossConfig.isBigBoss());
        builder.setKillScoreReward(GhostMgr.calBossKillScore(bossConfig, typeList, hpList));
        return builder;
    }

    public static GhostActivityProto.GhostUserDataMsg.Builder parseGhostUserDataMsg(GhostUserData userData) {
        GhostActivityProto.GhostUserDataMsg.Builder builder = GhostActivityProto.GhostUserDataMsg.newBuilder();
        builder.setEnergy(userData.getEnergy());
        builder.setLastRecoveryTime(userData.getLastRecoveryTime());
        builder.setEventType(userData.getEventType());
        builder.setEventParam(userData.getParam());
        builder.setComplete(userData.isComplete());
        builder.setEventTime(userData.getEventTime());
        builder.setIsGetDisperse(userData.isGetDisperse());
        builder.setBuffLevel(userData.getBuffLevel());
        builder.setScore(userData.getScore());
        builder.setBuyEnergyTimes(userData.getBuyEnergyTimes());
        for (GhostPatrons ghostPatrons : userData.getAttackMap().values()) {
            builder.addAttackTimesList(parseAttackTimesMsg(ghostPatrons));
        }
        builder.setIsGetCleanDisperse(userData.isGetCleanDisperse());
        return builder;
    }

    public static GhostActivityProto.AttackTimesMsg.Builder parseAttackTimesMsg(GhostPatrons ghostPatrons) {
        GhostActivityProto.AttackTimesMsg.Builder builder = GhostActivityProto.AttackTimesMsg.newBuilder();
        builder.setPatronsId(ghostPatrons.getId());
        builder.setCostAttackTimes(ghostPatrons.getCost());
        builder.setFreeAttackTimes(ghostPatrons.getFree());
        return builder;
    }
}
